﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Nivlag.VisualWebPart1
{
    
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/RenameNameSpace/VisualWebPart1/VisualWebPart1UserControl.ascx";

        VisualWebPart1UserControl userInterface;

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
            userInterface = (VisualWebPart1UserControl)control;
            
        } // CreateChildControls

        [ConnectionConsumer("Root site to analyze",AllowsMultipleConnections=true)]
        public void GetWPConnectedProviderInterface(IWebPartField connectProvider)
        {
            FieldCallback callback = new FieldCallback(ReceiveField);
            connectProvider.GetFieldValue(callback);
        } // GetWP...

        public void ReceiveField(object objField)
        {

            if (objField == null)
                return;

            string s = (string)objField;

            userInterface.SetReceivedField(s);

            SharePointTree spt = new SharePointTree("XmlSharePointData");

            string xmlTree = String.Empty;

            xmlTree = new RecursiveTraversOR(s).Traverse(s, xmlTree);

            string traverseResult = xmlTree;

            if (traverseResult == null) traverseResult = "fail";

            userInterface.SetReceivedField(xmlTree);

        }

    } // public class

} // namespace

